package povqs2;
import java.io.IOException;

import robocode.Event;
import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.ScannedRobotEvent;
import robocode.TeamRobot;
import robocode.util.Utils;

public class AbstractBoot extends TeamRobot {

	public int posRoboAdvX = 0;
	public int posRoboAdvY = 0;
	public double velRoboAdv = 0;
	public double angRobAdv = 45;
	public double enemyX = 0.0;
	public double enemyY = 0.0;
	public double distRoboAdv = 0;
	public static boolean DIREITA = false;
	public static boolean FRENTE = false;
	public static double DEGREES = 0.0;
	public static double DISTANCIA = 0.0;
	private double porcentagem = 0.5;
	public int timeScan = 0;
	public ScannedRobotEvent scanEvent;
	public boolean alternar = false;

	public void setaDadosRoboADV(ScannedRobotEvent e) {

		angRobAdv = e.getBearing();
		distRoboAdv = e.getDistance();
		velRoboAdv = e.getVelocity();

		double enemyBearing = this.getHeading() + e.getBearing();
		// Calculate enemy's position
		enemyX = getX() + e.getDistance()
				* Math.sin(Math.toRadians(enemyBearing));
		enemyY = getY() + e.getDistance()
				* Math.cos(Math.toRadians(enemyBearing));
	}
	
	public void setaDadosRoboADV(HitRobotEvent e) {

		angRobAdv = e.getBearing();
		distRoboAdv = 5;
		
		double enemyBearing = this.getHeading() + e.getBearing();
		// Calculate enemy's position
		enemyX = getX()	* Math.sin(Math.toRadians(enemyBearing));
		enemyY = getY()	* Math.cos(Math.toRadians(enemyBearing));
	}

	public void descobreDirecaoHorizontal() {

		if (alternar) {

			setDireita(angRobAdv);

		} else {

			setEsquerda(angRobAdv);
		}
	}
	
	public void avancar(){
		if (distRoboAdv < 200 && distRoboAdv > 20) {
			alternar = true;
			setTurnRight(angRobAdv + getGunCharge());
			setAhead(distRoboAdv);
			mira(angRobAdv + getGunCharge());
			fogo(distRoboAdv);
			// fire(2);
		} else if (distRoboAdv <= 20) {

			alternar = false;
			setTurnLeft(angRobAdv + getGunCharge());
			setAhead(distRoboAdv);
			mira(angRobAdv + getGunCharge());
			fogo(distRoboAdv);
			// fire(3);
		} else if (distRoboAdv >= 200 && distRoboAdv < 300) {

			alternar = true;
			setTurnRight(angRobAdv + getGunCharge());
			setAhead(distRoboAdv);
			mira(angRobAdv + getGunCharge());
			fogo(distRoboAdv);
			// fire(1);
		}
	}
	

	public double anguloRelativo(double ANG) {
		if (ANG > -180 && ANG <= 180)
			return ANG;
		double REL = ANG;
		while (REL <= -180)
			REL += 360;
		while (ANG > 180)
			REL -= 360;
		return REL;
	}

	public void mira(double Adv) {
		double A = getHeading() + Adv - getGunHeading();
		if (!(A > -180 && A <= 180)) {
			while (A <= -180)
				A += 360;
			while (A > 180)
				A -= 360;
		}
		turnGunRight(A);
	}

	public void dancinha() {
		setFrente(5);
		setDireita(360D);
		setTurnGunLeft(360D);
		setTurnRadarRight(30D);
	}

	public boolean pertoParede() {

		return (getX() < 50 || getX() > getBattleFieldWidth() - 50
				|| getY() < 50 || getY() > getBattleFieldHeight() - 50);
	}
	

	public void fogo(double Distancia) {

		if (Distancia > 200 || getEnergy() < 15)
			fire(1);
		else if (Distancia > 80)
			setFireBullet(2.5);
		else
			setFireBullet(3);
	}

	public void tiroFatal(double EnergiaIni) {
		double Tiro = (EnergiaIni / 4) + .1;
		fire(Tiro);
	}

	
	/*
	//codigo pode ser melhor
	
	angRobAdv = e.getBearing();
	setTurnRight(e.getBearing());
	setAhead(60);
	setTurnRight(e.getBearing()+50);
	setBack(90);
*/
	public void desviar(HitByBulletEvent e) {
		
		angRobAdv = e.getBearing();
		setDireita(e.getBearing()+5);
		setFrente(60);
		setDireita(e.getBearing()+50);
		setTraz(90);
	}
	
	public Pointer getLocation(){
		return new Pointer(getX(), getY());
	}
	
	public Pointer getLocationEnemy(){
		return new Pointer(enemyX, enemyY);
	}
	
	
	public void setFrente(double distance){
		FRENTE = true;
		this.DISTANCIA = distance;		
		setAhead(distance);
	}
	
	public void setTraz(double distance){
		FRENTE = false;
		this.DISTANCIA = distance;
		setBack(distance);
	}
	
	public void setDireita(double degrees){
		DIREITA = true;
		this.DEGREES = degrees;
		setTurnRight(degrees);
	}
	
	public void setEsquerda(double degrees){
		DIREITA = false;
		this.DEGREES = degrees;
		setTurnLeft(degrees);
	}
	
	public void monitoraRadar(){
		// turnRadarRightRadians(Double.MIN_NORMAL);
		if (timeScan++ > 1) {
			setTurnRadarRightRadians(Double.POSITIVE_INFINITY);

		}

		if (getOthers() == 0 && timeScan > 5) {
			onScannedRobot(scanEvent);
		}
	}
	
	public void posicionaRadar(ScannedRobotEvent e) {
		setTurnRadarRightRadians(Utils.normalRelativeAngle(getHeadingRadians()
				+ e.getBearingRadians() - getRadarHeadingRadians()) * 2);
	}
	
	public void msgTodosBoots(int status) {
		try {

			broadcastMessage(new Msg(new Pointer(enemyX, enemyY),new Pointer(getX(), getY()),distRoboAdv,status));

		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}
	
	public void movimentoContrario(){
		System.out.println("****************** Frente = "+FRENTE+" + "+ DISTANCIA);
		if(FRENTE){
			setTraz(50);
			System.out.println("****** traz"+ DISTANCIA);
		}
		else
		{
			setFrente(50);
			System.out.println("****** frente"+ DISTANCIA);
		}
		
		System.out.println("****************** Direita = "+DIREITA+" + "+ DEGREES);
		if(DIREITA){
			setEsquerda(90);
			
			System.out.println("****** esq "+ (DEGREES));
		}
		else
		{
			setDireita(90);
			System.out.println("****** dir "+ (DEGREES));
		}
	}
	
	public void msgUmBoot(String nomeBoot,int status){
		try {

			sendMessage(nomeBoot, new Msg(new Pointer(enemyX, enemyY),new Pointer(getX(), getY()),status));

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	public void aleatorioVertical(double distance){
		boolean rand = (((Math.random()*100)/2)==0);
		if(rand){
			setFrente(distance);
		}
		else{
			setTraz(distance);
		}
	}
	
	public void aleatorioHorizontal(double degrees){
		boolean rand = (((Math.random()*100)/2)==0);
		if(rand){
			setDireita(degrees);
		}
		else{
			setEsquerda(degrees);
		}
	}
	
	
	public double calcularAnguloAlvo(Pointer p){
		double dx = p.getX() - this.getX();
		double dy = p.getY() - this.getY();
		// Calculate angle to target
		double theta = Math.toDegrees(Math.atan2(dx, dy));
		return theta;
	}
	
}
